Ext.define('HIT.controller.ImportExport', {
	extend : 'Ext.app.Controller',

	models : [ 'FileModel' ],
	stores : [],
	views : [ 'UploadPanel' ],

	onButtonClick : function(button, e, options) {
		// console.log(button);
		if (button.id == 'set-account-import') {
			var check = Share.checkFun();
			if (check) {
				var win = new Ext.window.Window(
						{
							width : 650,
							title : Share.T('导入本地科目'),
							height : 400,
							layout : 'fit',
							modal : true,
							items : [ {
								xtype : 'fileuploadPanel',
								alldone : false,
								border : false,
								fileTypes : '*.xls',// 可上传文件类型
								uploadLimit : "1",
								postParams : {}, // http请求附带的参数
								uploadUrl : ctx
										+ '/io/account/upload;jsessionid='
										+ $.cookie('JSESSIONID'),
								flashUrl : ctx
										+ '/resources/swfupload/swfupload.swf',
								uploadSuccess : function(file, server_data) { // json
																				// string
									jsonObj = Ext.decode(server_data); // json
																		// object
									// console.log(jsonObj);
									if (jsonObj.resultcode == '0') {
										win.close();
										Share.ShowMessage("I0006","科目导入");
										Ext.getCmp('tab-set-account').getStore().reload();
									} else if (jsonObj.resultcode == '99') {
										Share.ShowMessage("E0011");
										win.close();
										return;
									} else {
										Share.ShowMessage("I0007","科目导入");
										return;
									}
								}
							} ]
						}).show();
			} else {
				Share.ShowMessage("E0026");
				return;
			}
		} else if (button.id == 'set-account-export') {
			window.location.href = ctx + '/io/account/download';
		} else if (button.id == 'set-assist-import') {
			var check = Share.checkFun();
			if (check) {
				var win = new Ext.window.Window({
					width : 650,
					title : Share.T('导入辅助配置'),
					height : 400,
					layout : 'fit',
					modal : true,
					items : [ {
						xtype : 'fileuploadPanel',
						alldone : false,
						border : false,
						fileTypes : '*.xls',// 可上传文件类型
						uploadLimit : "1",
						postParams : {}, // http请求附带的参数
						uploadUrl : ctx + '/io/assist/upload;jsessionid='
								+ $.cookie('JSESSIONID'),
						flashUrl : ctx + '/resources/swfupload/swfupload.swf',
						uploadSuccess : function(file, server_data) { // json
																		// string
							jsonObj = Ext.decode(server_data); // json object
							// console.log(jsonObj);
							if (jsonObj.resultcode == '0') {
								win.close();
								Share.ShowMessage("I0006","辅助配置导入");
								Ext.getCmp('tab-set-assist').getStore()
										.reload();
							} else if (jsonObj.resultcode == '99') {
								Share.ShowMessage("E0011");
								win.close();
								return;
							} else {
								Share.ShowMessage("I0007","辅助配置导入");
								return;
							}
						}
					} ]
				}).show();
			} else {
				Share.ShowMessage("E0026");
				return;
			}
		} else if (button.id == 'set-assist-export') {
			window.location.href = ctx + '/io/assist/download';
		} else if (button.id == 'set-tcode-import') {
			var check = Share.checkFun();
			if (check) {
				var win = new Ext.window.Window(
						{
							width : 650,
							title : Share.T('导入分析代码'),
							height : 400,
							layout : 'fit',
							modal : true,
							items : [ {
								xtype : 'fileuploadPanel',
								alldone : false,
								border : false,
								uploadLimit : "1",
								fileTypes : '*.xls',// 可上传文件类型
								postParams : {
									company_name : Ext.state.Manager
											.get('Cxjava_company')
								}, // http请求附带的参数
								uploadUrl : ctx
										+ '/io/tcode/upload;jsessionid='
										+ $.cookie('JSESSIONID'),
								flashUrl : ctx
										+ '/resources/swfupload/swfupload.swf',
								uploadSuccess : function(file, server_data) { // json
																				// string
									jsonObj = Ext.decode(server_data); // json
																		// object
									// console.log(jsonObj);
									if (jsonObj.resultcode == '0') {
										win.close();
										Share.ShowMessage("I0006","分析代码导入");
										Ext.getCmp('tab-set-tcode').getStore()
												.reload();
									} else if (jsonObj.resultcode == '99') {
										Share.ShowMessage("E0011");
										win.close();
										return;
									} else {
										Share.ShowMessage("I0007","分析代码导入");
										return;
									}
								}
							} ]
						}).show();
			} else {
				Share.ShowMessage("E0026");
				return;
			}
		} else if (button.id == 'set-tcode-export') {
			window.location.href = ctx + '/io/tcode/download';
		} else if (button.id == 'set-mapping-import') {
			var check = Share.checkFun();
			if (check) {
				var win = new Ext.window.Window({
					width : 650,
					title : Share.T('导入映射管理'),
					height : 400,
					layout : 'fit',
					modal : true,
					items : [ {
						xtype : 'fileuploadPanel',
						alldone : false,
						border : false,
						uploadLimit : "1",
						fileTypes : '*.xls',// 可上传文件类型
						postParams : {}, // http请求附带的参数
						uploadUrl : ctx
								+ '/io/accountMapping/upload;jsessionid='
								+ $.cookie('JSESSIONID'),
						flashUrl : ctx + '/resources/swfupload/swfupload.swf',
						uploadSuccess : function(file, server_data) { // json
																		// string
							jsonObj = Ext.decode(server_data); // json object
							// console.log(jsonObj);
							if (jsonObj.resultcode == '0') {
								win.close();
								Share.ShowMessage("I0006","科目映射导入");
								Ext.getCmp('tab-set-mapping').getStore()
										.reload();
							} else if (jsonObj.resultcode == '99') {
								Share.ShowMessage("E0011");
								win.close();
								return;
							} else {
								Share.ShowMessage("I0007","科目映射导入");
								return;
							}
						}
					} ]
				}).show();
			} else {
				Share.ShowMessage("E0026");
				return;
			}
		} else if (button.id == 'set-mapping-export') {
			window.location.href = ctx + '/io/accountMapping/download';
		} else if (button.id == 'mdata-client-import') {
			var check = Share.checkFun();
			if (check) {
				var win = new Ext.window.Window({
					width : 650,
					title : Share.T('导入客户主数据'),
					height : 400,
					layout : 'fit',
					modal : true,
					items : [ {
						xtype : 'fileuploadPanel',
						alldone : false,
						border : false,
						uploadLimit : "1",
						fileTypes : '*.xls;*.xlsx',// 可上传文件类型
						postParams : {}, // http请求附带的参数
						uploadUrl : ctx
								+ '/io/MdataClientMapping/upload;jsessionid='
								+ $.cookie('JSESSIONID'),
						flashUrl : ctx + '/resources/swfupload/swfupload.swf',
						uploadSuccess : function(file, server_data) { // json
																		// string
							jsonObj = Ext.decode(server_data); // json object
							// console.log(jsonObj);
							if (jsonObj.resultcode == '0') {
								win.close();
								Share.ShowMessage("I0006","客户主数据导入");
								Ext.getCmp('mdata-client-main-panel')
										.getStore().reload();
							} else if (jsonObj.resultcode == '-10') {
								Share.ShowMessage("E0016","客户代码");
								Ext.getCmp('mdata-client-main-panel')
										.getStore().reload();
								return;
							} else if (jsonObj.resultcode == '99') {
								Share.ShowMessage("E0011");
								win.close();
								return;
							} else {
								Share.ShowMessage("I0007","客户主数据导入");
								return;
							}
						}
					} ]
				}).show();
			} else {
				Share.ShowMessage("E0026");
				return;
			}
		} else if (button.id == 'mdata-client-export') {
			var Ids = [];
			var Address = null;
			var Client = null;
			var AddressNum = Ext.getCmp('mdata-client-detail-panel').getStore()
					.getCount();
			var Info = Ext.getCmp('mdata-client-detail-panel')
					.getSelectionModel().getSelection();
			var Main = Ext.getCmp('mdata-client-main-panel')
					.getSelectionModel().getSelection();
			if (Info.length < 1 && Main.length == 1 && AddressNum > 0) {// 已选基本信息(但不确定勾选几条)，未选联系信息，并且联系信息条数大于0
				Share.ShowMessage("I0011","需要导出的客户联系方式");
				return;
			}
			if (AddressNum > 0) {
				for ( var i = 0; i < Info.length; i++) {
					var Address2 = Info[i].get("id");// 获取勾选的联系信息
					Ids.push("'" + Address2 + "'");
				}
			}
			Ids.join(",");
			if (Main.length == 1) {// 若只勾选1条基本信息
				Address = AddressNum > 0 ? '(' + Ids + ')' : "NONE";
				Client = '('
						+ Ext.getCmp('mdata-client-main-panel')
								.getSelectionModel().getSelection()[0]
								.get("id") + ')';
			} else {// 若没勾选1条基本信息
				var Ids2 = [];
				Address = "NONE";
				if (Main.length > 0) {// 若勾选了N条基本信息
					for ( var i = 0; i < Main.length; i++) {
						var Address3 = Main[i].get("id");
						Ids2.push("'" + Address3 + "'");
					}
					Ids2.join(",");
					Client = '(' + Ids2 + ')';
				} else {// 若未勾选基本信息，则全部导出
					Client = "ALL";
				}
			}
			window.location.href = ctx + '/io/MdataClientMapping/' + Address
					+ '/' + Client + '/' + 'download';
		} else if (button.id == 'mdata-goods-import') {
			var check = Share.checkFun();
			if (check) {
				var win = new Ext.window.Window({
					width : 650,
					title : Share.T('导入商品主数据'),
					height : 400,
					layout : 'fit',
					modal : true,
					items : [ {
						xtype : 'fileuploadPanel',
						alldone : false,
						border : false,
						uploadLimit : "1",
						fileTypes : '*.xls;*.xlsx',// 可上传文件类型
						postParams : {}, // http请求附带的参数
						uploadUrl : ctx
								+ '/io/MdataGoodsMapping/upload;jsessionid='
								+ $.cookie('JSESSIONID'),
						flashUrl : ctx + '/resources/swfupload/swfupload.swf',
						uploadSuccess : function(file, server_data) { // json
																		// string
							jsonObj = Ext.decode(server_data); // json object
							// console.log(jsonObj);
							if (jsonObj.resultcode == '0') {
								win.close();
								Share.ShowMessage("I0006","商品主数据导入");
								Ext.getCmp('tab-mdata-goods').getStore()
										.reload();
							} else if (jsonObj.resultcode == '-10') {
								Share.ShowMessage("E0016","商品代码");
							} else if (jsonObj.resultcode == '99') {
								Share.ShowMessage("E0011");
								win.close();
								return;
							} else {
								Share.ShowMessage("I0007","商品主数据导入");
								return;
							}
						}
					} ]
				}).show();
			} else {
				Share.ShowMessage("E0026");
				return;
			}
		} else if (button.id == 'mdata-goods-export') {
			window.location.href = ctx + '/io/MdataGoodsMapping/download';
		} else if (button.id == 'mdata-account-import') {
			var check = Share.checkFun();
			if (check) {
				var win = new Ext.window.Window({
					width : 650,
					title : Share.T('导入销方银行账号主数据'),
					height : 400,
					layout : 'fit',
					modal : true,
					items : [ {
						xtype : 'fileuploadPanel',
						alldone : false,
						border : false,
						uploadLimit : "1",
						fileTypes : '*.xls;*.xlsx',// 可上传文件类型
						postParams : {}, // http请求附带的参数
						uploadUrl : ctx
								+ '/io/MdataAccountMapping/upload;jsessionid='
								+ $.cookie('JSESSIONID'),
						flashUrl : ctx + '/resources/swfupload/swfupload.swf',
						uploadSuccess : function(file, server_data) { // json
																		// string
							jsonObj = Ext.decode(server_data); // json object
							// console.log(jsonObj);
							if (jsonObj.resultcode == '0') {
								win.close();
								Share.ShowMessage("I0006","销方银行账号主数据导入");
								Ext.getCmp('tab-mdata-cancelaccount')
										.getStore().reload();
							} else if (jsonObj.resultcode == '-10') {
								Share.ShowMessage("E0016","相同的销方银行账号代码");
							} else if (jsonObj.resultcode == '99') {
								Share.ShowMessage("E0011");
								win.close();
								return;
							} else {
								Share.ShowMessage("I0007","销方银行账号主数据导入");
								return;
							}
						}
					} ]
				}).show();
			} else {
				Share.ShowMessage("E0026");
				return;
			}
		} else if (button.id == 'mdata-account-export') {
			window.location.href = ctx + '/io/MdataAccountMapping/download';
		} else if (button.id == 'set-rule-export') {
			// ■规则一览页面：导出按钮
			var tabpanelId = 'tab-set-rule';
			var tab = Ext.getCmp(tabpanelId);
			if (tab == undefined) {
				return;
			}
			var type = Ext.getCmp('type').getValue(); // 获得规则类型
			var cust_code = Ext.getCmp('cust_code').getRawValue()==""?"emptyCode":Ext.getCmp('cust_code').getRawValue(); // 客户Code
			var creator = Ext.getCmp('creator').getValue()==null?"emptyCreator":Ext.getCmp('creator').getValue(); // 创建人
			// 进行规则选择验证
			if (type == '00') {
				Share.ShowMessage("I0055");
				return;
			}
			// 规则类型
			// GET的方式到后台
			window.open(ctx + '/io/rule/SetRule/' + type + '/'+ cust_code + 
					'/'+ creator + '/' + 'download');
		}
      
		// ■规则一览页面：导入按钮
        else if(button.id == 'set-rule-import'){
            	var win = new Ext.window.Window({
                    width : 650,
                    title : Share.T('导入规则'),
                    height : 400,
                    layout : 'fit',
                    modal : true,
                    items : [ {
                        xtype : 'fileuploadPanel',
                        alldone: false,
                        border : false,
                        uploadLimit: "1",
                        fileTypes : '*.xls;*.xlsx',//可上传文件类型
                        postParams : {}, //http请求附带的参数
                        uploadUrl : ctx + '/io/SetRuleMapping/upload;jsessionid=' + $.cookie('JSESSIONID'),
                        flashUrl : ctx + '/resources/swfupload/swfupload.swf',
                        uploadSuccess : function(file, server_data) { // json string 
                            jsonObj = Ext.decode(server_data); // json object
                            if(jsonObj.resultcode == '0'){
                          	  win.close();
                          	  Share.ShowMessage("I0006","规则导入");
                          	  Ext.getCmp('tab-set-rule').getStore().reload();
                            }else if(jsonObj.resultcode == '2'){
                              Share.ShowMessage("E0052","导入规则文件中公司代码与当前登录公司代码不一致");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                          	  return; 
                            }else if(jsonObj.resultcode == '3'){
                              Share.ShowMessage("E0052","导入规则文件中有客户不存在");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '4'){
                              Share.ShowMessage("E0052","导入规则文件中有客户不存在并且公司ID与当前登录公司有不一致的情况");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '5'){
                              Share.ShowMessage("E0052","该文件不是规则文件");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '6'){
                              Share.ShowMessage("E0002","导入文件规则主数据读取");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '7'){
                              Share.ShowMessage("E0002","导入文件规则条件项目读取");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '8'){
                              Share.ShowMessage("E0002","导入文件规则处理内容读取");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '9'){
                              Share.ShowMessage("E0052","规则文件中客户主数据无任何数据");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '10'){
                              Share.ShowMessage("E0052","规则文件中规则处理内容无任何数据");
                              win.close();
                              Ext.getCmp('tab-set-rule').getStore().reload();
                              return; 
                            }else if(jsonObj.resultcode == '99'){
                              Share.ShowMessage("E0011");
                          	  win.close();
                          	  return;
                            } else {
                              Share.ShowMessage("I0007","规则导入");
                              return;
                            }
                        }
                    } ]
                }).show();
        }
	},

	init : function(application) {

		this.control({
			"#headpanel button" : {
				click : this.onButtonClick
			}
		});
	}
});
